<script>
 import { reactive, toRefs } from "vue";
import axios from "axios";
import { useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from 'element-plus'

export default {
  setup() {
    //实例化
    const router= useRouter()
    //定义需要绑定的数据
    const state = reactive({
        users:{
            userId: "",
            identityCard:"",
            password: "",
            confirmPassword:""
        }
    })

    //注册
    function updatepass(){
        //1.对数据进行非空校验
        if(state.users.userId==''){
            alert('手机号不能为空！');
            return;
        }
        if(state.users.userId.length<11){
            alert('手机号格式错误！');
            return;
        }
        if(state.users.identityCard==''){
            alert('身份证号不能为空！');
            return;
        }
        if(state.users.identityCard.length<18){
            alert('身份证号格式错误！');
            return;
        }
        if(state.users.password==''){
            alert('密码不能为空！');
            return;
        }
        if(state.users.password!=state.users.confirmPassword){
            alert('两次密码不同');
            return;
        }

        //2.axios提交请求给服务端
        //3.根据响应结果跳转或者报错
        axios.post('/api/users/updatepass',state.users).then((response)=>{     //  response = [ApiRestResponse]
            console.log(response.data)
            let apires=response.data
            if(apires.status==200){
                ElMessage({
                        type: 'success',
                        message: '修改密码成功！',
                    })
                router.push('/')
            }else{
                ElMessage({
                        type: 'error',
                        message: apires.desc,
                    })
                state.users.userId=''
                return;
            }
          
        }).catch((error)=>{
            console.log(error)
        })
        
    }
   

    return { //把数据和函数暴露出去
      ...toRefs(state),
      updatepass
    }
  }
}
</script>



<template>
 <!-- 总容器 -->
 <div class="wrapper">
        <header>
            <i class="fa fa-angle-left" onclick="history.go(-1)"></i>
            <p>修改密码</p>
            <div></div>
        </header>
        <div class="top-ban"></div>
        <h1>修改密码</h1>
        <table>
            <tr>
                <td>手机号码：</td>
                <td><input type="text" placeholder="请输入手机号码" v-model="users.userId"></td>
            </tr>
            <tr>
                <td>身份证号：</td>
                <td><input type="text" placeholder="请输入身份证号" v-model="users.identityCard"></td>
            </tr>
            <tr>
                <td>新密码：</td>
                <td><input type="password" placeholder="请输入密码" v-model="users.password"></td>
            </tr>
            <tr>
                <td>确认密码：</td>
                <td><input type="password" placeholder="请再次输入密码" v-model="users.confirmPassword"></td>
            </tr>
        </table>
        <div class="btn" @click="updatepass">完成</div>
    </div>
</template>



<style scoped>
 /*********************** 总容器 ***********************/
.wrapper{
    width: 100%;
    height: 100%;
}

/*********************** header ***********************/
header{
    width: 100%;
    height: 15.7vw;
    background-color: #FFF;

    position: fixed;
    left: 0;
    top: 0;

    display: flex;
    align-items: center;
    justify-content: space-between;

    box-sizing: border-box;
    padding: 0 3.6vw;
}
header .fa{
    font-size: 8vw;
}

/*********************** 标题部分 ***********************/
h1{
    font-size: 7.4vw;
    font-weight: 500;
    box-sizing: border-box;
    padding: 0 3.6vw;
    margin-top: 6vw;
}

/*********************** common样式 ***********************/
.top-ban{
    width: 100%;
    height: 15.7vw;
}

/*********************** table ***********************/
table{
    width: 92.8vw;
    margin: 0 auto;
    margin-top: 5vw;
    border-collapse: collapse;

    font-size: 4.2vw;
}
table tr td{
    height: 12vw;
    border-bottom: solid 1px #DDD;
}
table tr td input{
    border: none;
    outline: none;
}

/*********************** btn ***********************/
.btn{
    width: 92.8vw;
    margin: 0 auto;
    height: 12vw;
    margin-top: 8vw;
    background-color: #137E92;
    border-radius: 2vw;
    color: #FFF;
    font-size: 5vw;
    text-align: center;
    line-height: 12vw;

    user-select: none;
    cursor: pointer;
}
</style>